MESSAGEBOX("VAI COMECAR")
USE C:\MaisEscola_Path\Impacto\Dados\TAB_TAB_GRATUR
CREATE CURSOR A_TURPRO(TURMA C(06),GUERRA C(12),QTD N(04))

SELECT TAB_TAB_GRATUR
SET ORDER TO GRATUR3
GO TOP
DO WHILE NOT EOF()
   IF STR(TAB_TAB_GRATUR.ESCOLA,3)+TAB_TAB_GRATUR.ANO+TAB_TAB_GRATUR.SEQANO="  120051"
      WTURMA =TAB_TAB_GRATUR.COD
      IF NOT EMPTY(TAB_TAB_GRATUR.PROFES)
         WGUERRA=TAB_TAB_GRATUR.PROFES
         
         SELECT A_TURPRO
         LOCATE FOR A_TURPRO.GUERRA+A_TURPRO.TURMA=WGUERRA+WTURMA
         IF EOF()
            APPEND BLANK
            REPLACE TURMA  WITH WTURMA  ,;
                    GUERRA WITH WGUERRA ,;
                    QTD    WITH 1
         ELSE
            REPLACE QTD    WITH QTD+1
         ENDIF
      ENDIF
      IF NOT EMPTY(TAB_TAB_GRATUR.PROFES1)
         WGUERRA=TAB_TAB_GRATUR.PROFES1
            
         SELECT A_TURPRO
         LOCATE FOR A_TURPRO.GUERRA=WGUERRA
         IF EOF()
            APPEND BLANK
            REPLACE GUERRA WITH WGUERRA ,;
                    QTD    WITH QTD+1
         ELSE
            REPLACE QTD    WITH QTD+1
         ENDIF
      ENDIF

      IF NOT EMPTY(TAB_TAB_GRATUR.PROFES2)
         WGUERRA=TAB_TAB_GRATUR.PROFES2
   
         SELECT A_TURPRO
         LOCATE FOR A_TURPRO.GUERRA=WGUERRA
         IF EOF()
            APPEND BLANK
            REPLACE GUERRA WITH WGUERRA ,;
                    QTD    WITH QTD+1
         ELSE
            REPLACE QTD    WITH QTD+1
         ENDIF
      ENDIF

      IF NOT EMPTY(TAB_TAB_GRATUR.PROFES3)
         WGUERRA=TAB_TAB_GRATUR.PROFES3

         SELECT A_TURPRO
         LOCATE FOR A_TURPRO.GUERRA=WGUERRA
         IF EOF()
            APPEND BLANK
            REPLACE GUERRA WITH WGUERRA ,;
                    QTD    WITH QTD+1
         ELSE
            REPLACE QTD    WITH QTD+1
         ENDIF
      ENDIF
      
      IF NOT EMPTY(TAB_TAB_GRATUR.PROFES4)
         WGUERRA=TAB_TAB_GRATUR.PROFES4
      
         SELECT A_TURPRO
         LOCATE FOR A_TURPRO.GUERRA=WGUERRA
         IF EOF()
            APPEND BLANK
            REPLACE GUERRA WITH WGUERRA ,;
                    QTD    WITH QTD+1
         ELSE
            REPLACE QTD    WITH QTD+1
         ENDIF
      ENDIF
   ENDIF
   SELECT TAB_TAB_GRATUR
   SKIP IN "TAB_TAB_GRATUR"
ENDDO

MESSAGEBOX("CURSOR FINAL")

SELECT GUERRA,SUM(QTD) AS TOTAL ;
   FROM A_TURPRO        ;
   INTO CURSOR A_TURPRO ;
   GROUP BY GUERRA      ;
   ORDER BY TOTAL DESC
BROW

MESSAGEBOX("FINAL")